package com.pay.ioopos.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aggregate.pay.sanstar.enums.PayStatus;
import com.aggregate.pay.sanstar.enums.PayType;
import com.pay.ioopos.R;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatisticsPayAdapter extends ArrayAdapter<Map<String, Object>> {
    private Map<PayType, Bitmap> bitmaps = new HashMap<>();
    private int resource;
    public StatisticsPayAdapter(Context context, int resource, List<Map<String, Object>> objects) {
        super(context, resource, objects);
        this.resource = resource;
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.pay_type);
        bitmaps.put(PayType.UNIONPAY, Bitmap.createBitmap(bitmap, 125, 125, 112, 110));
        bitmaps.put(PayType.ALIPAY, Bitmap.createBitmap(bitmap, 5, 5, 112, 110));
        bitmaps.put(PayType.WEIXIN, Bitmap.createBitmap(bitmap, 125, 5, 112, 110));
        bitmaps.put(PayType.QQ, Bitmap.createBitmap(bitmap, 125, 245, 112, 110));
        bitmaps.put(PayType.BAIDU, Bitmap.createBitmap(bitmap, 245, 5, 112, 110));
        bitmaps.put(PayType.JD, Bitmap.createBitmap(bitmap, 5, 245, 112, 110));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Map<String, Object> map = getItem(position);

        View view = LayoutInflater.from(getContext()).inflate(resource,null);

        TextView num = view.findViewById(R.id.num);
        num.setText((position >= 9 ? "": "0")  + (position + 1) + ".");

        ImageView payType = view.findViewById(R.id.pay_type);
        payType.setLayoutParams(new LinearLayout.LayoutParams(32, 32));
        payType.setImageBitmap(bitmaps.get(PayType.values()[Integer.parseInt(String.valueOf(map.get("payType")))]));

        TextView amount = view.findViewById(R.id.amount);
        PayStatus status = PayStatus.values()[Integer.parseInt(String.valueOf(map.get("status")))];
        if (status == PayStatus.NEW || status == PayStatus.PAYING) {
            amount.setTextColor(getContext().getColor(R.color.colorWarning));
        } else if (status == PayStatus.FAIL || status == PayStatus.REPEAL ) {
            amount.setTextColor(getContext().getColor(R.color.colorDanger));
        } else if (status == PayStatus.SUCCESS || status == PayStatus.REPEAL ) {
            amount.setTextColor(getContext().getColor(R.color.colorSuccess));
        }
        amount.setText(new BigDecimal(String.valueOf(map.get("amount"))).setScale(2, RoundingMode.DOWN).toPlainString());

        TextView addTime = view.findViewById(R.id.add_time);
        addTime.setText(new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date(Long.parseLong(String.valueOf(map.get("addTime"))))));

        return view;
    }
}
